/*
 * $Id: GraphDialog.java,v 1.1 2003/05/17 15:33:29 znamenacek Exp $
 *
 * Copyright (c) Milan Jedlicka
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.gui;

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;

import net.sf.vofce.export.ImageExporter;

import java.util.ResourceBundle;

/**
 * Dialog which sets up graph size and other properties.
 *  
 * @author Milan Jedlicka
 */
public class GraphDialog extends JDialog {

	/** Localised resource strings. */
	private static ResourceBundle resources;
	
	/** Minimum pixel width */
	public static final int MIN_PX_WIDTH = 100;
	/** Maximum pixel width */
	public static final int MAX_PX_WIDTH = 10000;

	/** Minimum pixel height */
	public static final int MIN_PX_HEIGHT = 100;
	/** Maximum pixel height */
	public static final int MAX_PX_HEIGHT = 10000;

	/** Inches per centimeter */
	public static final float INCHES_PER_CM = 2.54f;

	/** Image exporter instance */
	private ImageExporter ie;
	
	int pixelWidth = 400;
        int pixelHeight = 400;

	// GUI parts
	
	protected JButton ok;
        protected JButton cancel;
	
	public static final Integer[] DPI_LIST = {
		new Integer(72),
		new Integer(75),
		new Integer(100),
		new Integer(300),
		new Integer(360),
		new Integer(600),
		new Integer(720),
		new Integer(1200)
	};

	WholeNumberField pixelWidthField = new WholeNumberField(pixelWidth, 8);
        WholeNumberField pixelHeightField = new WholeNumberField(pixelHeight, 8);

	JTextField cmWidthField = new JTextField("10", 8);
        JTextField cmHeightField = new JTextField("5", 8);

	JRadioButton printerButton;
        JRadioButton screenButton;

	JComboBox dpiList = new JComboBox(DPI_LIST);
	
	/**
	 * Default constructor 
	 * @param ie <code>ImageExporter</code> to set up
	 */
	public GraphDialog(JDialog parent, ImageExporter ie) {
		super(parent, resources.getString("graph.title"));
		this.ie = ie;
		
		getContentPane().setLayout(new GridBagLayout());

		// Buttons
		Box buttonBar = Box.createHorizontalBox();
		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		buttonPanel.add(buttonBar, BorderLayout.CENTER);
		ok = new JButton(resources.getString("button.ok"));
		cancel = new JButton(resources.getString("button.cancel"));
	
		cancel.addActionListener(new CancelListener());
                ok.addActionListener(new OKListener());

                buttonBar.add(Box.createHorizontalGlue());
		buttonBar.add(cancel);
		buttonBar.add(Box.createHorizontalStrut(10));
		buttonBar.add(ok);

		// GUI
		JPanel configPanel = new JPanel();
		configPanel.setBorder(new TitledBorder(new EtchedBorder(), resources.getString("graph.sizeoptions")));

		Box screenExport = Box.createVerticalBox();
		Box printerExport = Box.createVerticalBox();
		
		configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.Y_AXIS));

		configPanel.add(screenExport);
                configPanel.add(printerExport);

		ButtonGroup styleSwitcher = new ButtonGroup();
		
		screenButton = new JRadioButton(resources.getString("graph.screen"), true);
                screenButton.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		styleSwitcher.add(screenButton);
                screenExport.add(screenButton);

		printerButton = new JRadioButton(resources.getString("graph.printer"));
                styleSwitcher.add(printerButton);
		printerExport.add(printerButton);

		// Screen export
		JPanel fieldWrapper = new JPanel(new FlowLayout());
		fieldWrapper.add(new JLabel(resources.getString("graph.size") + ": "));
		fieldWrapper.add(pixelWidthField);
                fieldWrapper.add(new JLabel(" x "));
                fieldWrapper.add(pixelHeightField);
	        fieldWrapper.add(new JLabel(" px"));
		fieldWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
		screenExport.add(fieldWrapper);

		// Printer export
		JPanel fieldWrapper2 = new JPanel(new FlowLayout());
		fieldWrapper2.add(new JLabel(resources.getString("graph.size") + ": "));
		fieldWrapper2.add(cmWidthField);
		fieldWrapper2.add(new JLabel(" x "));
		fieldWrapper2.add(cmHeightField);
		fieldWrapper2.add(new JLabel(" cm"));
		fieldWrapper2.setAlignmentX(Component.LEFT_ALIGNMENT);
		printerExport.add(fieldWrapper2);

		JPanel fieldWrapper3 = new JPanel(new FlowLayout());
		dpiList.setSelectedIndex(5);

		fieldWrapper3.add(new JLabel(resources.getString("graph.resolution") + ": "));
		fieldWrapper3.add(dpiList);
		fieldWrapper3.add(new JLabel("dpi"));
		fieldWrapper3.setAlignmentX(Component.LEFT_ALIGNMENT);
		printerExport.add(fieldWrapper3);
	
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.insets = new Insets(10, 10, 10, 10);
		
		getContentPane().add(configPanel, gridBagConstraints);
		
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		getContentPane().add(buttonPanel, gridBagConstraints);
	
		// Adjust size
		pack();
		setSize(getPreferredSize());
		setResizable(false);

		// Center
		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int) ((screenDim.width - getWidth()) / 2), (int) ((screenDim.height - getHeight()) / 2));
	}
		
	protected boolean validateInput() {
		int pxWidth;
                int pxHeight;

		float cmWidth = 0;
                float cmHeight = 0;

		boolean screen = screenButton.isSelected();

		if (screen) {
			pxWidth = pixelWidthField.getValue();
                        pxHeight = pixelHeightField.getValue();
		} else {
			cmWidth = Float.parseFloat(cmWidthField.getText());
			cmHeight = Float.parseFloat(cmHeightField.getText());

			int dpi = ((Integer)dpiList.getSelectedItem()).intValue();
			
			pxWidth = cmToPixels(cmWidth, dpi);
			pxHeight = cmToPixels(cmHeight, dpi);
		}

		if ((pxWidth < MIN_PX_WIDTH) || (pxWidth > MAX_PX_WIDTH)) {
			showError(resources.getString("graph.wrongvalue") + ": " + (screen ? pxWidth : cmWidth));
			(screen ? pixelWidthField : cmWidthField).requestFocus();
			return false;
		} 

		if ((pxHeight < MIN_PX_HEIGHT) || (pxHeight > MAX_PX_HEIGHT)) {
			showError(resources.getString("graph.wrongvalue") + ": " + (screen ? pxHeight : cmHeight));
			(screen ? pixelHeightField : cmHeightField).requestFocus();
			return false;
		}

		pixelWidth = pxWidth;
                pixelHeight = pxHeight;
		ie.setPixelWidth(pxWidth);
		ie.setPixelHeight(pxHeight);
		
		return true;
	}

	public void show() {
		getRootPane().setDefaultButton(ok);
		super.show();
	}
	
	protected void showError(String text) {
		JOptionPane.showMessageDialog(
		this,
		text,
		resources.getString("graph.miscerror"), 
		JOptionPane.ERROR_MESSAGE
		);
	}
	
	static public int cmToPixels(float cm, int dpi) {
		return Math.round(cm/INCHES_PER_CM) * dpi;
	}

	protected class OKListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (!validateInput()) {
				return;
			}

			dispose();
		}
	}

	protected class CancelListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}

        static {
		resources = ResourceBundle.getBundle("net.sf.vofce.gui.resources");
	}
}
